import sys
import logging
import puzautils.table.puza_row as pr
import puzautils.table.puza_memory_table as pmt
import puzautils.table.puza_stream_table as pst
import puzautils.twitter_api.twitter_crawler as tc

seed_table_name = sys.argv[1]
save_table_name = sys.argv[2]
log_filename = sys.argv[3]

OutputTable = pst.PuzaStreamTable()
OutputTable.OutputFile(save_table_name)

if __name__ == '__main__':
    logging.basicConfig(filename= log_filename, filemode='w', level=logging.INFO, format='%(asctime)s::%(levelname)s::[%(module)s::%(funcName)s]::%(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
    c = tc.TwitterCrawler('wume7')
    c.Auth()
    ## read seeds
    SeedTable = pmt.PuzaMemoryTable()
    SeedTable.InputFile(seed_table_name)
    SeedTable.Load()
    for i in range(len(SeedTable)):
        s_id = SeedTable[i]['id']
        logging.info('Start to handle status:'+str(s_id))
        status = c.GetStatus(s_id)
        if status != None:
            r = pr.PuzaRow.Load(status)
            OutputTable.StreamOutRow(r, lformat = 0, block_number = 1)
        logging.info('Finish to handle status:'+str(s_id))
    OutputTable.StreamClose()
    logging.info('Finished.')